package xiaojun.JAVA.service.array;

import xiaojun.JAVA.domain.dto.RoleDto;
import xiaojun.JAVA.domain.emtity.Role;
import xiaojun.JAVA.domain.vo.RoleVo;
import xiaojun.JAVA.domain.vo.Result;
import xiaojun.JAVA.service.IRoleService;
import xiaojun.JAVA.service.IUserService;

/**
 * 业务层-角色
 *
 * @author 赖晓君
 * @since 2025/6/5  08:59
 */
public class RoleService extends BaseService<Role,RoleDto,RoleVo>implements IRoleService {

    private IUserService userService;

    private static IRoleService singleton;

    private RoleService(){
        data = new Role[DATA_SIZE+1];
        temp = new RoleVo[DATA_SIZE+1];
    }

    public static IRoleService getInstance(){
        if(singleton == null){
            singleton = new RoleService();
        }
        return singleton;
    }

    @Override
    RoleVo convertToVo(Role role){
        return new RoleVo(role.getId(),role.name,role.dateScope);
    }

    @Override
    Role createEntity(RoleDto dto,int id){
        return new Role(id, dto.name,dto.dateScope);
    }

    @Override
    void updateEntity(RoleDto dto,Role role) {
        role.name = dto.name;
        role.dateScope = dto.dateScope;
    }



    @Override
    public Result delete(int id) {
        if (!existsById(id)) {
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService == null){
            userService = UserService.getInstance();
        }
        if(userService.roleInUse(id)){
            return Result.failure("删除失败，还有用户具有该角色，不能删除");
        }
        data[id] = null;
        return Result.success("删除成功",null);
    }
}
